// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import { DragonEvent } from '../define/BattleDefine';

const { ccclass, property } = cc._decorator;

@ccclass
export default class DragonNode extends cc.Node {
  public armatureDisplay: dragonBones.ArmatureDisplay = this.addComponent(dragonBones.ArmatureDisplay);
  private _isReady: boolean = false;
  private _animList: Array<string>;
  private _animName: string;
  private _playTimes: number;
  public resUrl: string;
  public _eventList: Array<any> = [];
  // LIFE-CYCLE CALLBACKS:

  // onLoad () {}

  start() {}

  addEventListener(type: string, callback: (event: any) => void, target?: any, useCapture?: boolean) {
    if (this._isReady) {
      this.armatureDisplay.addEventListener(type, this._eventHandler, this);
    } else {
      this._eventList.push([type, callback, target, useCapture]);
      //this.armatureDisplay.addEventListener(type, this._eventHandler, this);
    }

    this.once(type, callback, this, useCapture);
  }

  /**
   * 这个地方因为做的时候 死活收不到addEVENT complete 得回调 只能在onReady得时候 执行这个
   * @param type
   * @param callback
   * @param target
   * @param useCapture
   */
  pushEventListerner(type: string, callback: (event: any) => void, target?: any, useCapture?: boolean) {
    this._eventList.push([type, callback, target, useCapture]);
    //this.on(type, this._eventHandler, this, useCapture);
  }

  removeEventListener(type: string, callback: (event: any) => void, target?: any, useCapture?: boolean) {
    if (this._isReady) {
      this.armatureDisplay.removeEventListener(type, this._eventHandler, this);
    }
    this.off(type, callback, target, useCapture);
  }
  _eventHandler(event) {
    //this.emit(event);
    this.dispatchEvent(event);
  }
  startReady() {
    this._isReady = false;
  }
  onReady() {
    this._isReady = true;
    this._animList = this.armatureDisplay.getAnimationNames(this.armatureDisplay.armatureName);
    for (var i = 0; i < this._eventList.length; i++) {
      if (this._eventList[i][0] == DragonEvent.loadReady) {
        this._eventList[i][1].apply(this._eventList[i][2]);
      } else {
        this.armatureDisplay.addEventListener(this._eventList[i][0], this._eventHandler, this);
      }
    }
    this._eventList = [];

    if (this._animName != null) {
      this.playAnimation(this._animName, this._playTimes);
    }

    //cc.log("onReady")
    this.emit('onReady');
  }
  playAnimation(animName: string, playTimes: number) {
    this._animName = animName;
    this._playTimes = playTimes;
    if (!this._isReady) return;
    if (this._animList.indexOf(animName) == -1) {
      cc.log(this._animList);
      cc.log('播放不存在的动画 ：' + animName);
      return;
    }
    this.armatureDisplay.playAnimation(animName, playTimes);
  }

  stopAnimation() {
    let armature = this.armatureDisplay.armature();
    armature.animation.stop();
  }

  get getAnimation(): dragonBones.ArmatureDisplay {
    return this.armatureDisplay;
  }

  // update (dt) {}
}
